package com.yc.thread;

public class Demo1 {

    public static void main(String[] args) throws InterruptedException {
        // 主线程
        Thread thread = Thread.currentThread();
        long id = thread.getId();
        String name = thread.getName();
        System.out.println(name + ":" + id);

        // 多态：  引用转型
        Thread t = new Saodi("扫地线程");

        // start 一执行，后面的代码不会等待当前run方法执行结束， 就会立即执行
        Thread t1 = new Thread(new Cachuang());

        t.setPriority(10);
        t1.setPriority(1);

        // 设置精灵线程
        t.setDaemon(true);
        t1.setDaemon(true);

        // 启动线程
        t.start();
        t1.start();

        // 线程休眠 100 毫秒
        Thread.sleep(100);
        System.out.println("--------------- 主线程执行完成 -----------------");

    }


}

// 继承Thread
class Saodi extends Thread {

    public Saodi(String name) {
        super(name);
    }

    @Override
    public void run() {
        Thread thread = Thread.currentThread();
        long id = thread.getId();
        String name = thread.getName();
        for (int i = 0; i < 1000; i++) {
            System.out.println(name + ":" + id + " === " + i);
        }
    }
}

// 实现 Runnable 接口
class Cachuang implements Runnable {
    @Override
    public void run() {
        Thread thread = Thread.currentThread();
        long id = thread.getId();
        String name = thread.getName();
        for (int i = 0; i < 1000; i++) {
            System.out.println("===" + name + ":" + id + "===" + i);
        }
    }
}
